package com.tengju.bff.interfaces.covercharge.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.idanchuang.abmio.feign.client.FileServiceFeignClient;
import com.idanchuang.abmio.feign.dto.FileInfoDTO;
import com.idanchuang.component.base.JsonResult;
import com.tengju.bff.interfaces.covercharge.entity.bo.FileInfoBo;
import com.tengju.bff.interfaces.covercharge.service.IAttachmentService;
import com.tengju.bff.interfaces.covercharge.util.BeanMapperUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import org.springframework.util.StringUtils;

/**
 * @author liang_shi
 * @date 2020/8/27 14:45
 * @description 商城商品库图片服务
 */
@Slf4j
@Service
public class AttachmentServiceImpl implements IAttachmentService {

    @Autowired
    private FileServiceFeignClient fileServiceFeignClient;

    /**
     * 获取文件信息
     *
     * @param fileId 文件Id
     */
    @Override
    public String getUrl(String fileId) {
        String url = "";
        try {
            if (StringUtils.isEmpty(fileId) || fileId.startsWith("http")){
                return fileId;
            }
            List<Long> reqId = Lists.newArrayList(Long.valueOf(fileId));
            JsonResult<List<FileInfoDTO>> fileResList = fileServiceFeignClient.batchQuery(reqId);
            log.info("基础服务-获取文件信息结束,reqId:{},result:{}", reqId, JSON.toJSONString(fileResList));
            if (Objects.isNull(fileResList) || !fileResList.isSuccess() || CollectionUtils.isEmpty(fileResList.getData())) {
                return url;
            }
            List<FileInfoDTO> fileInfoDTOList = fileResList.getData();
            url = BeanMapperUtils.map(fileInfoDTOList.get(0), FileInfoBo.class).getUrl();
        } catch (Exception e) {
            log.error("基础服务-获取文件信息异常:", e);
        }
        return url;
    }


}
